/**
 * 扫描模型
 * Author: void rangers
 * Versions: 1.0.0
 * Date: 2021-5-14
 * Oright: https://gitee.com/void_rangers/amap
 * **/

;
!function(win){
    function CreateObject3D(map, options){
        this.map = map;     //地图实例
        this.default = {
            debug: false,   //把相关Object3D实例更新至控制台
            lnglat: null,   //lnglat对象
            radius: 200,    //半径, 米
            unit: 1,        //绘制的单位
            range: 200,     //绘制的次数
            rgb: [103, 222, 211],   //绘制区域的颜色
            transparent: true,      //是否使用透明颜色，并进行颜色混合
            backOrFront: 'front',   //显示Mesh类型的面(front: 正面, back: 反面, both: 两面)
        };
        this.opts = Object.assign({}, this.default, options);
        //获取当前地图的分辨率 进行换算 所绘圆形半径
        this.radius = this.opts.radius / this.map.getResolution(this.opts.lnglat, 20);

        //在地图上创建 Object3DLayer 图层 (承载 Object3D 实例的图层)
        this.object3Dlayer = new AMap.Object3DLayer();
        this.map.add(this.object3Dlayer);

        this.radar = new AMap.Object3D.Mesh();  //创建 Mesh 对象
        this.geometry = this.radar.geometry;    //创建描述 Mesh 的几何信息对象

        this.init();

        return this;
    }

    CreateObject3D.prototype = {
        init(){
            this.radar.transparent = this.opts.transparent;
            this.radar.backOrFront = this.opts.backOrFront;
            if(this.opts.debug){
                win.map = this.map;
                win.radar = this.radar;
                win.geometry = this.geometry;
            }
            console.log('Start Create Object3D:', this);

            this.createGeometry();
        },
        createGeometry(){
            let unit = this.opts.unit,
                count = this.opts.range / unit,
                r = this.getRgb(0),
                g = this.getRgb(1),
                b = this.getRgb(2);
            
            //生成 Mesh 对象的几何信息
            for(var i = 0; i < count; i++){
                var angle1 = i * unit * Math.PI / 180,
                    angle2 = (i + 1) * unit * Math.PI / 180,
                    x1 = Math.cos(angle1) * this.radius,
                    y1 = Math.sin(angle1) * this.radius,
                    x2 = Math.cos(angle2) * this.radius,
                    y2 = Math.sin(angle2) * this.radius;
                
                this.geometry.vertices.push(0, 0, 0);
                this.geometry.vertices.push(x1, y1, 0);
                this.geometry.vertices.push(x2, y2, 0);

                this.geometry.vertexColors.push(r, g, b, this.getOpacity(i / count));
                this.geometry.vertexColors.push(r, g, b, this.getOpacity(i / count));
                this.geometry.vertexColors.push(r, g, b, this.getOpacity((i + 1) / count));
            }
            //固定 Mesh 的中心位置
            this.radar.position(this.opts.lnglat);
            //把 Mesh 添加到图层上
            this.object3Dlayer.add(this.radar);
            //扫描启动
            this.animationScan();
        },
        getRgb(idx){
            return this.opts.rgb[idx] / 255;
        },
        getOpacity(scale){
            return 1 - Math.pow(scale, 0.3);
        },
        //生成一个扫描动画
        animationScan(){
            this.radar.rotateZ(-2);
            AMap.Util.requestAnimFrame(this.animationScan.bind(this));
        }
    };
    win.CreateObject3D = CreateObject3D;
}(window);